import java.util.HashMap;
import java.util.Map;

/**
 * @author : xwj
 * @date : 2021/7/20 20:47
 * @description :
 * @class : test13
 **/

public class test13 {

    public static int romanToInt(String s) {
        int result = 0,i = 0;
        HashMap<String,Integer> str = new HashMap<String,Integer>(){{
        put("I",1);
        put("V",5);
        put("X",10);
        put("L",50);
        put("C",100);
        put("D",500);
        put("M",1000);
        }};
        while (i < s.length() - 1){
            if (str.get(String.valueOf(s.charAt(i))) < str.get(String.valueOf(s.charAt(i+1)))){
                result = result + str.get(String.valueOf(s.charAt(i+1))) - str.get(String.valueOf(s.charAt(i)));
                i = i + 2;
                if (i == s.length() - 1){
                    result = result + str.get(String.valueOf(s.charAt(i)));
                    i++;
                }
            } else {
                result = result + str.get(String.valueOf(s.charAt(i)));
                i++;
            }
        }
        if (i == s.length()-1){
            result = result + str.get(String.valueOf(s.charAt(i)));
        }
        return result;
    }

    public static void main(String[] args) {
        String s = "MDCXCV";
        System.out.println(romanToInt(s));
    }

}
